package com.zy.pro.calendar.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zy.core.model.PageView;
import com.zy.core.web.BaseController;
import com.zy.pro.calendar.model.Calendar;
import com.zy.pro.calendar.service.CalendarService;
import com.zy.pro.calendar.web.form.CalendarSearchForm;
import com.zy.pro.shop.model.Shop;
import com.zy.pro.shop.service.ShopService;
import com.zy.pro.system.web.form.MenuForm;
import com.zy.utils.DateHelp;
/**
 * 工作日历管理控制器
 *
 */
@Controller
@RequestMapping("/control/calendar/calendarController")
public class CalendarController extends BaseController {

	private static final Logger logger = Logger.getLogger(CalendarController.class);
	
	/**
	 * 工作日历管理服务类
	 */
	@Resource(name = "calendarServiceImpl")
	private CalendarService calendarService;
	
	/**
	 * 店铺管理服务类
	 */
	@Resource(name = "shopServiceImpl")
	private ShopService shopService;
	
	/**
	 * 卖家中心  - 卖家工作日历
	 * @param calendar
	 * @param map
	 * @return
	 */
	@RequestMapping("/displayCalendar")
	public String displayCalendar(Calendar calendar, ModelMap map){
		try {
			
			//设置菜单类型
			MenuForm menuForm = new MenuForm(MenuForm.MENU_TYPE_SELLER,MenuForm.PAR_TYPE_SELLER_INFO,MenuForm.SUB_TYPE_SELLER_INFO_TASK);
			map.addAttribute("menuForm", menuForm);
			
			Shop shop = shopService.find(calendar.getShopId());
			map.addAttribute("shop", shop);
			return "control/jsp/seller/task";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	
	/**
	 * 卖家中心 - 添加工作日历管理
	 */
	@RequestMapping("/addCalendar")
	@ResponseBody
	public JSON addCalendar(CalendarSearchForm form,String date ,ModelMap map) {
		JSONObject obj = new JSONObject();
		obj.put("status", false);
		obj.put("id", null);
		try {
			//设置状态日期
			form.setCalendarDate(DateHelp.fmtStrToDate(date));
			//判断该日期下是否已经设置有工作状态,如果已经存在工作状态则不再进行设置
			List<Calendar> cList = calendarService.findAll(form);
			if(cList.isEmpty()){
				//无,则设置
				Calendar calendar = new Calendar();
				//设置店铺主键
				calendar.setShopId(form.getShopId());
				//设置状态日期
				calendar.setCalendarDate(DateHelp.fmtStrToDate(date));
				//创建时间
				calendar.setCalendarCreateTime(new Date());
				//插入数据
				calendarService.save(calendar);
				obj.put("status", true);
				obj.put("id", calendar.getCalendarId());
			}
			return obj;
		} catch(Exception e){
			logger.error(e);
			return obj;
		}
	}
	
	/**
	 * 卖家中心 - 删除工作日历管理
	 */
	@RequestMapping("/deleteCalendar")
	@ResponseBody
	public JSON deleteCalendar(Calendar calendar, ModelMap map) {
		JSONObject obj = new JSONObject();
		obj.put("status", false);
		obj.put("id", null);
		obj.put("date", null);
		try {
			Calendar c = calendarService.find(calendar.getCalendarId());
			if(c != null){
				calendarService.delete(calendar.getCalendarId());
				obj.put("status", true);
				obj.put("id", calendar.getCalendarId());
				obj.put("date", DateHelp.fmtDateToStr(c.getCalendarDate(), "yyyy-MM-dd"));
			}
			return obj;
		} catch(Exception e){
			logger.error(e);
			return obj;
		}
	}
	
	/**
	 * 卖家中心 - 检验是否已经存在工作状态
	 *//*
	@RequestMapping("/checkCalendar")
	@ResponseBody
	public JSON checkCalendar(CalendarSearchForm form,String date ,ModelMap map) {
		JSONObject obj = new JSONObject();
		obj.put("status", false);
		obj.put("id", null);
		try {
			//设置状态日期
			form.setCalendarDate(DateHelp.fmtStrToDate(date));
			
			List<Calendar> cList = calendarService.findAll(form);
			if(!cList.isEmpty()){
				obj.put("status", true);
				obj.put("id", cList.get(0).getCalendarId());
			}
			return obj;
		} catch(Exception e){
			logger.error(e);
			return obj;
		}
	}*/
	
	
	/**
	 * 列出工作日历管理
	 */
	@RequestMapping("/calendarList")
	@ResponseBody
	public List<JSONObject> calendarList(CalendarSearchForm form,String searchDate, ModelMap map) {
		List<JSONObject> objList = new ArrayList<JSONObject>();
		try {
			
			form.setCalendarDateStart(DateHelp.addDay(DateHelp.getMonthStart(DateHelp.fmtStrToDate(searchDate)),-7));
			form.setCalendarDateEnd(DateHelp.addDay(DateHelp.getLastDayOfMonth(DateHelp.fmtStrToDate(searchDate)),15));
			
			//设置查询排序
			form.setCalendarDate(DateHelp.fmtStrToDate(searchDate));
			//判断该日期下是否已经设置有工作状态,如果已经存在工作状态则不再进行设置
			List<Calendar> cList = calendarService.findListByDate(form);
			if(!cList.isEmpty()){
				for (Calendar calendar : cList) {
					JSONObject obj = new JSONObject();
					obj.put("status", true);
					obj.put("id", calendar.getCalendarId());
					obj.put("date", DateHelp.fmtDateToStr(calendar.getCalendarDate(), "yyyy-MM-dd"));
					objList.add(obj);
				}
			}
			return objList;
		} catch(Exception e){
			logger.error(e);
			return objList;
		}
	}
	
	
	
	/**
	 * 列出工作日历管理
	 */
	@RequestMapping("/list")
	public String list(CalendarSearchForm form, ModelMap map) {
		try {
			//设置查询排序
			//TODO eg:form.getOrderby().put("id", "asc");
			
			
			//创建页面对象
			PageView<Calendar> pageView = new PageView<Calendar>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(calendarService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 显示添加工作日历管理
	 */
	@RequestMapping("/displayAdd")
	public String displayAdd(Calendar calendar, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			
			
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	
	
	/**
	 * 显示编辑工作日历管理
	 */
	@RequestMapping("/displayEdit")
	public String displayEdit(CalendarSearchForm form, ModelMap map) {
		try {
			//TODO 查询对象
			//eg:Calendar calendar = calendarService.find(form.getId());
			//eg:map.addAttribute("entity", calendar);
			
			
			//TODO 转发页面
			return "";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 编辑工作日历管理
	 */
	@RequestMapping("/edit")
	public String edit(Calendar Calendar, ModelMap map) {
		try {
			//TODO:处理工作日历管理
			
			
			calendarService.updateBySelective(Calendar);
			return list(new CalendarSearchForm(), map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}

	/**
	 * 删除单个工作日历管理
	 */
	@RequestMapping("/deletecalendar")
	public String delete(CalendarSearchForm form, ModelMap map) {
		try {
			//TODO  eg:calendarService.delete(form.getId());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 批量删除工作日历管理
	 */
	@RequestMapping("/batchDelete")
	public String deletes(CalendarSearchForm form, ModelMap map) {
		try {
//			TODO  eg:calendarService.batchDelete(form.getIds());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
}
